<template>
  <el-container class="repassword-container">
    <el-header class="repassword-header">
      <h2 style="margin-top: 0px">重置用户密码</h2>
    </el-header>

    <el-main class="repassword-main">
      <el-form :model="form" :rules="rules" ref="formRef">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" clearable />
        </el-form-item>


        <el-button type="primary" @click="resetPassword">重置密码（默认为123456）</el-button>
      </el-form>
    </el-main>
  </el-container>
</template>

<script>
import { ref } from "vue";
import axios from "axios";

export default {
  setup() {
    const form = ref({
      username: "",
    });
    const formRef = ref(null);

    // 验证规则
    const rules = {
      username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
    };

    // 重置密码方法
    const resetPassword = async () => {
      formRef.value.validate(async (valid) => {
        if (valid) {
          try {
            const response = await axios.get("/user/user/rePassword.api", {
              params: { userName: form.value.username },
            });

            // 假设接口返回一个成功信息
            if (response.data.success) {
              this.$message.success("密码重置成功");
            } else {
              this.$message.error(response.data.message || "重置密码失败");
            }
          } catch (error) {
            this.$message.error("请求失败，请重试");
            console.error(error);
          }
        }
      });
    };

    return { form, rules, formRef, resetPassword };
  },
};
</script>

<style scoped>
.repassword-container {
  padding: 20px;
}

.repassword-header {
  text-align: center;
  background-color: #409eff;
  color: white;
  padding: 10px;
  margin-bottom: 20px;

}

.repassword-main {
  background: #f4f7fc;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.el-form-item {
  margin-bottom: 20px;
}

.el-button {
  width: 100%;
}
</style>
